﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Reporting.WinForms;
using System.Data;
using BUS;
using DTO;
namespace QuanLySach
{
    /// <summary>
    /// Interaction logic for BaoCaoTonUserControl.xaml
    /// </summary>
    public partial class BaoCaoTonUserControl : UserControl
    {
        private Section_DTO Section;
        private MainWindow _parent;

        public BaoCaoTonUserControl(Section_DTO sec, MainWindow par)
        {
            Section = sec;
            _parent = par;
            InitializeComponent();
        }

        private void UserControl_Loaded_1(object sender, RoutedEventArgs e)
        {
            TaoBaoCao();
        }

        /// <summary>
        /// Ham tao bao cao va luu bao cao xuong CSDL
        /// </summary>
        private void TaoBaoCao()
        {

            // Lay ngay tao bao cao
            //***********************************************
            // Tao lay ngay he thong de test
            // May thiet ke lai cho chon thang bao cao
            // Cai nay co rat nhieu van de phat sinh.Thu 2 noi
            string day = System.DateTime.Now.ToString();

            // Tao bang bao cao Ton
            DataTable baocao = BaoCaoTon_BUS.BAOCAOTON.TaoBaoCaoTon(day);

            // Luu du lieu bao cao xuong CSDL
            ThemBaoCao(day, baocao);

            // Load giao dien bao cao cho nguoi dung
            LoadBaoCao();

        }

        /// <summary>
        /// Su dung Microsoft ReportView xuat bao cao cho nguoi dung
        /// </summary>
        private void LoadBaoCao()
        {

            ReportDataSource reportDataSource = new ReportDataSource();
            QLNSDataSet dataset = new QLNSDataSet();
            System.Windows.Forms.BindingSource binding = new System.Windows.Forms.BindingSource();

            dataset.BeginInit();
            reportDataSource.Name = "DataSetBCT";
            binding.DataMember = "TaoBaoCaoTon";
            binding.DataSource = dataset;
            reportDataSource.Value = binding;
            this.reportBCCN.LocalReport.DataSources.Add(reportDataSource);
            this.reportBCCN.LocalReport.ReportPath = "../../BCTReport.rdlc";
            dataset.EndInit();

            QLNSDataSetTableAdapters.TaoBaoCaoTonTableAdapter
            accountsTableAdapter = new QLNSDataSetTableAdapters.TaoBaoCaoTonTableAdapter();

            accountsTableAdapter.ClearBeforeFill = true;
            accountsTableAdapter.Fill(dataset.TaoBaoCaoTon, System.DateTime.Now);
            this.reportBCCN.RefreshReport();
        }

        /// <summary>
        /// Them Bao Cao va Chi Tiet Bao Cao vao CSDL
        /// </summary>
        /// <param name="day">Thang bao cao</param>
        /// <param name="chitiet">Bang chi tiet bao cao</param>
        private void ThemBaoCao(string day, DataTable chitiet)
        {
            // Them Bao Cao Cong No vao co so du lieu
            BaoCaoTon_DTO bc = new BaoCaoTon_DTO(day);
            BaoCaoTon_BUS.BAOCAOTON.ThemBaoCaoTon(bc);

            // Them Chi Tiet Bao Cao vao co so du lieu
            int ma = BaoCaoTon_BUS.BAOCAOTON.LayMaBaoCaoTonLonNhat();
            
            int r = chitiet.Rows.Count;
            
            for (int i = 0; i < r; i++)
            {
                int masach = Int32.Parse(chitiet.Rows[i]["MaSach"].ToString());
                int tondau = Int32.Parse(chitiet.Rows[i]["TonDau"].ToString());
                int phatsinh = Int32.Parse(chitiet.Rows[i]["PhatSinh"].ToString());
                int toncuoi = Int32.Parse(chitiet.Rows[i]["TonCuoi"].ToString());

                ChiTietBaoCaoTon_DTO ct = new ChiTietBaoCaoTon_DTO(ma, masach, tondau, phatsinh, toncuoi);
                ChiTietBaoCaoTon_BUS.CHITIETBAOCAOTON.ThemChiTietBaoCaoTon(ct);
            }
        }
    }
}
